package com.test.dababy.dababy_06_loan_apply;

import static com.test.dababy.dababy_00_utils.DababyConfigs.Debug;
import static com.test.dababy.dababy_00_utils.DababyUtil.startLoading;
import static com.test.dababy.dababy_00_utils.DababyUtil.stopLoading;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.test.dababy.R;
import com.test.dababy.dababy_00_customer_service.Customer_dababyServiceDialog;
import com.test.dababy.dababy_00_http.OkHttpUtil;
import com.test.dababy.dababy_04_home.MainAct_dababyivity;

import java.util.ArrayList;

public class LoanApplyActivity_OldUser extends AppCompatActivity {


    private boolean oMNJ=true;
    private float jDRF=0.17393112f;
    private char eF20='E';
    private double zGEM=0.8375394549215409;
    private boolean pd1b=true;
    private double yAp8=0.2765102454864842;
    private String v8nI="";
    private int d8Gp=137;
    private double cbWc=0.15849902550567052;
    private String u3ba="";
    private int p363=23;
    private boolean nXDP=true;

    public void mLBDI(){
        new Thread(() -> {
            try {

                if (TextUtils.isEmpty(getV8nI())) setV8nI(String.valueOf(isoMNJ())+getjDRF()+geteF20()+getzGEM()+isPd1b()+getyAp8());
                else setU3ba(String.valueOf(getD8Gp())+getCbWc()+getP363()+isnXDP());

            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setoMNJ(boolean oMNJ) {
        this.oMNJ = oMNJ;
    }

    public void setjDRF(float jDRF) {
        this.jDRF = jDRF;
    }

    public void seteF20(char eF20) {
        this.eF20 = eF20;
    }

    public void setzGEM(double zGEM) {
        this.zGEM = zGEM;
    }

    public void setPd1b(boolean pd1b) {
        this.pd1b = pd1b;
    }

    public void setyAp8(double yAp8) {
        this.yAp8 = yAp8;
    }

    public void setV8nI(String v8nI) {
        this.v8nI = v8nI;
    }

    public void setD8Gp(int d8Gp) {
        this.d8Gp = d8Gp;
    }

    public void setCbWc(double cbWc) {
        this.cbWc = cbWc;
    }

    public void setU3ba(String u3ba) {
        this.u3ba = u3ba;
    }

    public void setP363(int p363) {
        this.p363 = p363;
    }

    public void setnXDP(boolean nXDP) {
        this.nXDP = nXDP;
    }

    public boolean isoMNJ() {
        return oMNJ;
    }

    public float getjDRF() {
        return jDRF;
    }

    public char geteF20() {
        return eF20;
    }

    public double getzGEM() {
        return zGEM;
    }

    public boolean isPd1b() {
        return pd1b;
    }

    public double getyAp8() {
        return yAp8;
    }

    public String getV8nI() {
        return v8nI;
    }

    public int getD8Gp() {
        return d8Gp;
    }

    public double getCbWc() {
        return cbWc;
    }

    public String getU3ba() {
        return u3ba;
    }

    public int getP363() {
        return p363;
    }

    public boolean isnXDP() {
        return nXDP;
    }

    private TextView amountTv;
    private TextView amountTitleTv;
    private SeekBar amountSb;
    private View rangeView;
    private TextView minAmountTv,maxAmountTv;
    private LinearLayout itemGroupLayout;

    private LoanDayChooseViewAdapter dayChooseViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dababy_loan_apply_old_user);
        if (!Debug) getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        SwipeRefreshLayout refreshLayout = findViewById(R.id.adlaou_refreshlayout);
        refreshLayout.setOnRefreshListener(() -> {
            refreshLayout.setRefreshing(false);
            OkHttpUtil.getInstance_dababy().uploadEventPoint("APPLY_2","REFRESH_PULL","");
            startLoading(this,getString(R.string.calculating));
            OkHttpUtil.getInstance_dababy().fetchLnAplDetail_OldUser(this::refreshLoanApplyDetail);
        });

        findViewById(R.id.adlaou_ib_back).setOnClickListener(v -> onBackPressed());
        findViewById(R.id.adlaou_ib_cs).setOnClickListener(v -> {
            OkHttpUtil.getInstance_dababy().uploadEventPoint("APPLY_2","CUSTOMER_SERVICE_CLICK","");
            new Customer_dababyServiceDialog(this).show();
        });

        amountTv = findViewById(R.id.adlaou_tv_amount);
        amountTitleTv = findViewById(R.id.adlaou_tv_amount_title);
        amountSb = findViewById(R.id.adlaou_sb_amount);
        rangeView = findViewById(R.id.adlaou_view_range);
        minAmountTv = findViewById(R.id.adlaou_tv_min);
        maxAmountTv = findViewById(R.id.adlaou_tv_max);
        RecyclerView termRv = findViewById(R.id.adlaou_rv_term);
        itemGroupLayout = findViewById(R.id.adlaou_layout_item_group);
        Button submitBtn = findViewById(R.id.adlaou_btn_submit);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);//网格布局管理器
        gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
        termRv.setLayoutManager(gridLayoutManager);
        dayChooseViewAdapter = new LoanDayChooseViewAdapter(chooseDay -> {
            startLoading(this,getString(R.string.calculating));
            OkHttpUtil.getInstance_dababy().uploadEventPoint("APPLY_2","DAY_CHOOSE_CLICK",String.valueOf(chooseDay));
            int maxAmount = LoanApplyDetail_OldUser.getInstance_dababy().getMaxAmountNum();
            int minAmount = LoanApplyDetail_OldUser.getInstance_dababy().getMinAmountNum();
            int granularity = LoanApplyDetail_OldUser.getInstance_dababy().getGranularityNum();
            String chooseAmount = String.valueOf(amountSb.getProgress()*granularity);
            OkHttpUtil.getInstance_dababy().fetchLnAplDetail_OldUser(
                    String.valueOf(maxAmount),
                    String.valueOf(minAmount),
                    chooseAmount,
                    String.valueOf(chooseDay), //天数
                    this::refreshLoanApplyDetail
            );

        });
        termRv.setAdapter(dayChooseViewAdapter);

        submitBtn.setOnClickListener(v -> {
            startLoading(this,"");
            OkHttpUtil.getInstance_dababy().uploadEventPoint("APPLY_2","SUBMIT_CLICK","");

            int amount = LoanApplyDetail_OldUser.getInstance_dababy().getAmount();
            if (amount<=0){
                Toast.makeText(this,getString(R.string.apply_tips_no_amount_tips),Toast.LENGTH_SHORT).show();
                return;
            }

            OkHttpUtil.getInstance_dababy().aplyLn_OldUser(
                    String.valueOf(amount),
                    String.valueOf(dayChooseViewAdapter.getChooseDay()),
                    this::onLnAplyResp_dababy);
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        startLoading(this,getString(R.string.calculating));
        OkHttpUtil.getInstance_dababy().uploadEventPoint("APPLY_2","START","");
        OkHttpUtil.getInstance_dababy().fetchLnAplDetail_OldUser(this::refreshLoanApplyDetail);
    }

    @SuppressLint("SetTextI18n")
    public void refreshLoanApplyDetail(){

        stopLoading();

        int amount = LoanApplyDetail_OldUser.getInstance_dababy().getAmount();
        boolean canChooseAmount = LoanApplyDetail_OldUser.getInstance_dababy().getChooseAmountFlag();
        int maxAmount = LoanApplyDetail_OldUser.getInstance_dababy().getMaxAmountNum();
        int minAmount = LoanApplyDetail_OldUser.getInstance_dababy().getMinAmountNum();
        int granularity = LoanApplyDetail_OldUser.getInstance_dababy().getGranularityNum();
        int[] dayArr = LoanApplyDetail_OldUser.getInstance_dababy().getDaysArray();

        //显示额度
        amountTv.setText(LoanApplyDetail_OldUser.getInstance_dababy().getAmountWithCurrency());
        //显示天数
        dayChooseViewAdapter.setDays(dayArr);
        dayChooseViewAdapter.setChooseDay(LoanApplyDetail_OldUser.getInstance_dababy().getDays());
        //是否可选金额
        if (canChooseAmount){
            amountTitleTv.setText(getString(R.string.apply_ou_choose_amount));
            amountSb.setVisibility(View.VISIBLE);
            rangeView.setVisibility(View.VISIBLE);

            //刷新额度选择控件
            amountSb.setMax(maxAmount/granularity);
            amountSb.setMin(minAmount/granularity);
            amountSb.setProgress(amount/granularity);
            minAmountTv.setText(String.valueOf(minAmount));
            maxAmountTv.setText(String.valueOf(maxAmount));
            amountSb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    amountTv.setText(String.valueOf(progress*granularity));
                }
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {}
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    startLoading(LoanApplyActivity_OldUser.this,getString(R.string.calculating));
                    String chooseAmount = String.valueOf(seekBar.getProgress()*granularity);
                    OkHttpUtil.getInstance_dababy().uploadEventPoint("APPLY_2","AMOUNT_CHOOSE",chooseAmount);
                    OkHttpUtil.getInstance_dababy().fetchLnAplDetail_OldUser(
                            String.valueOf(maxAmount),
                            String.valueOf(minAmount),
                            chooseAmount,
                            String.valueOf(dayChooseViewAdapter.getChooseDay()), //天数
                            () -> refreshLoanApplyDetail()
                    );
                }
            });

        }else {
            amountTitleTv.setText(getString(R.string.loan_amount));
            amountSb.setVisibility(View.GONE);
            rangeView.setVisibility(View.GONE);
        }

        mLBDI();

        refreshParamView_dababy();
    }

    @SuppressLint("InflateParams")
    private void refreshParamView_dababy(){

        itemGroupLayout.removeAllViews();

        ArrayList<LnoaAp_dababyl_dababyyItem> params = LoanApplyDetail_OldUser.getInstance_dababy().getDetailItemList_dababy();
        View groupView = null;
        LinearLayout groupLayout = null;

        for (LnoaAp_dababyl_dababyyItem lnAplyItem:params){

            if (lnAplyItem.isTitle_dababy()){
                if (groupView!=null && groupLayout!=null) itemGroupLayout.addView(groupView);
                groupView = LayoutInflater.from(this).inflate(R.layout.view_dababy_item_bg_dababy_group,null);
                groupLayout = groupView.findViewById(R.id.vibg_layout_group);
                addItem(groupLayout,lnAplyItem);
            }else {
                if (groupView!=null && groupLayout!=null) addItem(groupLayout,lnAplyItem);
            }

        }

        itemGroupLayout.addView(groupView);

    }

    private void addItem(LinearLayout group, LnoaAp_dababyl_dababyyItem lnAplyItem){

        @SuppressLint("InflateParams") View item = LayoutInflater.from(this).inflate(R.layout.item_dababy_lnaply_detail,null);

        TextView nameTv = item.findViewById(R.id.ilda_tv_name);
        TextView valueTv = item.findViewById(R.id.ilda_tv_value);

        nameTv.setText(lnAplyItem.getShowName_dababy());
        valueTv.setText(lnAplyItem.getShowDetail_dababy());

        if (lnAplyItem.isTitle_dababy()){
            nameTv.setTextColor(Color.WHITE);
            nameTv.setTypeface(Typeface.DEFAULT_BOLD);
            valueTv.setTextColor(Color.parseColor("#FF3F8CFF"));
            valueTv.setTypeface(Typeface.DEFAULT_BOLD);
        }

        group.addView(item);
    }

    public void onLnAplyResp_dababy(){
        OkHttpUtil.getInstance_dababy().requestUsPrcs(this::onProcessResp_dababy);
    }

    public void onProcessResp_dababy(){
        stopLoading();
        Intent intent = new Intent(this, MainAct_dababyivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("APPLY_2","END","");
    }

}